package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.user.IdentityAccountDAO;
import tt.dz.biz.dao.user.OnlineAccountDAO;
import tt.dz.biz.dao.user.UserDAOImpl;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.DzErrorCode.UserErrorCode;
import tt.dz.vo.IdentityAccountVO;
import tt.dz.vo.OnlineAccountVO;
import tt.dz.vo.UserBaseVO;

@Service("rpcQueryCardsService")
public class RPCQueryCardsService
  implements RPCBaseService
{
  private static final Logger log = LoggerFactory.getLogger(RPCQueryCardsService.class);
  @Resource(name="identityAccountDAO")
  private IdentityAccountDAO identityAccountDAO;
  @Resource(name="userDAO")
  private UserDAOImpl userDAOImpl;
  @Resource(name="onlineAccountDAO")
  private OnlineAccountDAO onlineAccountDAO;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
    throws Exception
  {
    Object req = ((GenericRecord)request).get("req");
    
    JSONObject jsonObj = JSON.parseObject(req.toString());
    String tmpUserId = jsonObj.getString("userId");
    
    JSONObject resultJson = new JSONObject();
    resultJson.put("isSuccess", Integer.valueOf(0));
    if (StringUtils.isBlank(tmpUserId))
    {
      resultJson.put("errorMsg", DzErrorCode.CommonErrorCode.PARAM_REQUIRED.getDesc());
      resultJson.put("errorCode", DzErrorCode.CommonErrorCode.PARAM_REQUIRED.value());
    }
    UserBaseVO bVo = new UserBaseVO();
    bVo.setUserId(tmpUserId);
    
    OnlineAccountVO oaCard = this.onlineAccountDAO.queryOnlineCardByUser(tmpUserId);
    if (oaCard == null)
    {
      resultJson.put("errorMsg", DzErrorCode.UserErrorCode.USER_NOT_FOUND.getDesc());
      resultJson.put("errorCode", DzErrorCode.UserErrorCode.USER_NOT_FOUND.value());
    }
    else
    {
      try
      {
        List<IdentityAccountVO> list = this.identityAccountDAO.queryIdentityAccountByUser(bVo);
        JSONObject card = new JSONObject();
        card.put("cardNo", oaCard.getCardNo());
        card.put("cardType", oaCard.getCardType());
        card.put("state", oaCard.getState());
        
        List<JSONObject> cardList = new ArrayList();
        cardList.add(card);
        
        JSONObject identifyCard = null;
        for (IdentityAccountVO o : list)
        {
          identifyCard = new JSONObject();
          identifyCard.put("cardNo", o.getCardNo());
          identifyCard.put("cardType", o.getCardType());
          identifyCard.put("state", o.getState());
          cardList.add(identifyCard);
        }
        resultJson.put("cardList", cardList);
        resultJson.put("isSuccess", Integer.valueOf(1));
      }
      catch (Exception e)
      {
        log.error("查询关联卡失败", e);
        resultJson.put("errorMsg", DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc());
        resultJson.put("errorCode", DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value());
      }
    }
    return resultJson.toJSONString();
  }
}
